package com.scs.application.core.utils;

import com.scs.application.modules.sys.security.UserPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;

/**
 * @description:
 */
@Slf4j
public class UserUtils {

    /**
     * 当前用户。 如果未登录，返回匿名用户实例
     * @return
     */
    public static UserPrincipal currentUser(){
        UserPrincipal userPrincipal = null;
        try{
            Object principal = SecurityUtils.getSubject().getPrincipal();
            userPrincipal =  (UserPrincipal) principal;
        }catch (UnavailableSecurityManagerException e) {
            log.info("UserUtils.currentUser.不重要的异常");
        }catch (Exception e){
            e.printStackTrace();
        }
        if (userPrincipal == null) {
            userPrincipal = new UserPrincipal();
            userPrincipal.setCode("system").setWorkNo("system").setName("系统");
        }
        return userPrincipal;
    }

    public static boolean isAdmin() {
        return currentUser().isAdmin();
    }
}
